package com.mt.skyhoist.util.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MyRadioGroup extends RadioGroup {
    private final String TAG = "laocuo";

    private int checkId;
    private ControlConfirmDialog dialog;

    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        dialog = new ControlConfirmDialog(getContext());
        dialog.setConfirmListener(new ControlConfirmDialog.OnBtnClickListener() {
            @Override
            public void Onclick(View view) {
                check(checkId);
            }
        });
//        dialog = new AlertDialog.Builder(getContext())
//                .setTitle("")
//                .setMessage("message")
//                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface anInterface, int i) {
//                        Log.d(TAG, "setPositiveButton");
//                        check(checkId);
//                    }
//                })
//                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface anInterface, int i) {
//                        Log.d(TAG, "setNegativeButton");
//                    }
//                })
//                .create();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int count = getChildCount();
        for (int i=0;i<count;i++) {
            RadioButton child = (RadioButton) getChildAt(i);
            int x = (int) ev.getX();
            int y = (int) ev.getY();
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getTop();
            int bottom = child.getBottom();
            Log.d(TAG, "x="+x+"|x="+x);
            Log.d(TAG, "left="+left+"|right="+right);
            Log.d(TAG, "top="+top+"|bottom="+bottom);
            if (x < right && x > left && y > top && y < bottom) {
                checkId = child.getId();
                String title = "";

                switch (i) {
                    case 0:
                        title = "SIDE A";
                        break;
                    case 1:
                        title = "CONTROL BOTH";
                        break;
                    case 2:
                        title = "SIDE B";
                        break;
                }

                dialog.setMessage("SELECTING "+title+" MOTOR CONTROL");
//                dialog.setMessage("SELECTING "+title+" MOTOR CONTROL");
                break;
            }
        }
        if (getCheckedRadioButtonId() != checkId) {
            dialog.show();
        }
        return true;
    }
}